﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2019-07-05 15:07:54
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class WxOfficialAccountMenu:IWxOfficialAccountMenu
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.CGTConnectionString;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(MenuId),0)+1
                               FROM [WxOfficialAccountMenu]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private WxOfficialAccountMenuInfo GetByReader(IDataReader dr)
         {
            WxOfficialAccountMenuInfo info = new WxOfficialAccountMenuInfo();
            object obj;
            obj=dr["MenuId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuId = Convert.ToInt32(obj);
            }
            obj=dr["MenuName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuName = obj.ToString();
            }
            obj=dr["MenuType"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuType = Convert.ToInt32(obj);
            }
            obj=dr["MenuParentId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuParentId = Convert.ToInt32(obj);
            }
            obj=dr["MenuKey"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuKey = obj.ToString();
            }
            obj=dr["MenuAppId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuAppId = obj.ToString();
            }
            obj=dr["MenuPagePath"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuPagePath = obj.ToString();
            }
            obj=dr["MenuUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuUrl = obj.ToString();
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = Convert.ToDateTime(obj);
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = Convert.ToInt32(obj);
            }
            return info;
         }
		public int Insert(WxOfficialAccountMenuInfo info)
		{
        
        
            string strSql=@"INSERT INTO WxOfficialAccountMenu			
			(			
			[MenuName],
			[MenuType],
			[MenuParentId],
			[MenuKey],
			[MenuAppId],
			[MenuPagePath],
			[MenuUrl],
			[CreatedDate],
			[IsActive]
			)
			 VALUES
			(			
			@MenuName,
			@MenuType,
			@MenuParentId,
			@MenuKey,
			@MenuAppId,
			@MenuPagePath,
			@MenuUrl,
			@CreatedDate,
			@IsActive
			)
			 SET @MenuId=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@MenuId",DbType.Int32, sizeof(Int32));  
            db.AddInParameter(dbc, "@MenuName",DbType.String, info.MenuName);
            db.AddInParameter(dbc, "@MenuType",DbType.Int32, info.MenuType);
            db.AddInParameter(dbc, "@MenuParentId",DbType.Int32, info.MenuParentId);
            db.AddInParameter(dbc, "@MenuKey",DbType.String, info.MenuKey);
            db.AddInParameter(dbc, "@MenuAppId",DbType.String, info.MenuAppId);
            db.AddInParameter(dbc, "@MenuPagePath",DbType.String, info.MenuPagePath);
            db.AddInParameter(dbc, "@MenuUrl",DbType.String, info.MenuUrl);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"MenuId");
         
		}
		
		
        public void Update(WxOfficialAccountMenuInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE WxOfficialAccountMenu SET ");
            sbSql.AppendLine("[MenuName] = @MenuName,");
                
            sbSql.AppendLine("[MenuType] = @MenuType,");
                
            sbSql.AppendLine("[MenuParentId] = @MenuParentId,");
                
            sbSql.AppendLine("[MenuKey] = @MenuKey,");
                
            sbSql.AppendLine("[MenuAppId] = @MenuAppId,");
                
            sbSql.AppendLine("[MenuPagePath] = @MenuPagePath,");
                
            sbSql.AppendLine("[MenuUrl] = @MenuUrl,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[IsActive] = @IsActive");
                
            sbSql.Append(" where  [MenuId] = @MenuId");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@MenuId",DbType.Int32, info.MenuId);
            db.AddInParameter(dbc, "@MenuName",DbType.String, info.MenuName);
            db.AddInParameter(dbc, "@MenuType",DbType.Int32, info.MenuType);
            db.AddInParameter(dbc, "@MenuParentId",DbType.Int32, info.MenuParentId);
            db.AddInParameter(dbc, "@MenuKey",DbType.String, info.MenuKey);
            db.AddInParameter(dbc, "@MenuAppId",DbType.String, info.MenuAppId);
            db.AddInParameter(dbc, "@MenuPagePath",DbType.String, info.MenuPagePath);
            db.AddInParameter(dbc, "@MenuUrl",DbType.String, info.MenuUrl);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int menuId)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [WxOfficialAccountMenu]  ");
            strSql.Append("WHERE [MenuId]=@MenuId ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@MenuId",DbType.Int32, menuId);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<WxOfficialAccountMenuInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<WxOfficialAccountMenuInfo> list = new List<WxOfficialAccountMenuInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "WxOfficialAccountMenu");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    WxOfficialAccountMenuInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public WxOfficialAccountMenuInfo GetByID(int menuId)
		{
            Model.WxOfficialAccountMenuInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [WxOfficialAccountMenu] ");
            strSql.Append("WHERE [MenuId]=@MenuId ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@MenuId",DbType.Int32, menuId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}